package __web

import (
	"log"
	"net/http"
	"time"
)

func hello(w http.ResponseWriter, _ *http.Request) {
	w.Write([]byte("Hello"))
}

// 中间件
func middleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		timeStart := time.Now()

		// next handler
		next.ServeHTTP(w, r)

		timeElapsed := time.Since(timeStart)
		log.Println(timeElapsed)
	})
}

func MiddleWare() {
	http.Handle("/", middleware(http.HandlerFunc(hello)))
	err := http.ListenAndServe(":8080", nil) // Handle 用来自定义路由，nil表示默认
	if err != nil {
		log.Fatal(err)
	}
}
